//
//  UILabel+XSExtension.m
//  jiaoquaner
//
//  Created by 郝旭珊 on 2017/10/28.
//  Copyright © 2017年 yimofang. All rights reserved.
//

#import "UILabel+XSExtension.h"

@implementation UILabel (XSExtension)

+ (instancetype)labelWithFrame:(CGRect)frame textFont:(UIFont *)font andTextColor:(UIColor *)color
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.textColor = color;
    label.font = font;
    label.numberOfLines = 0;
    return label;
}


+ (instancetype)labelWithFrame:(CGRect)frame
                          text:(NSString *)text
                      textFont:(UIFont *)font
                     textColor:(UIColor *)textColor
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.font = font;
    label.textColor = textColor;
    label.numberOfLines = 0;
    return label;
}


/**
 * 首行缩进 Label
 */
+ (instancetype)labelWithFirstIndent:(CGFloat)indent
                               frame:(CGRect)frame
                                text:(NSString *)text
                            textFont:(UIFont *)font
                           textColor:(UIColor *)textColor
                     backgroundColor:(UIColor *)backgroundColor
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.font = font;
    label.textColor = textColor;
    label.backgroundColor = backgroundColor;
    
    //段落样式
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    //首行文本缩进
    paraStyle.firstLineHeadIndent = indent;
    //使用文本段落样式
    NSDictionary *dict = @{NSParagraphStyleAttributeName:paraStyle};
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:text attributes:dict];
    label.attributedText = attr;
    return label;
}



+ (instancetype)labelWithTextFont:(UIFont *)font andTextColor:(UIColor *)color
{
    UILabel *label = [[UILabel alloc] init];
    label.textColor = color;
    label.font = font;
    label.numberOfLines = 0;
    return label;
}


+(instancetype)labelWithOrigin:(CGPoint)origin
                         text:(NSString *)text
                     textFont:(UIFont *)font
                    textColor:(UIColor *)textColor
{
    UILabel *label = [[UILabel alloc] init];
    label.text = text;
    label.font = font;
    
    CGSize size = CGSizeMake(320,2000); //设置一个行高上限
    NSDictionary *attribute = @{NSFontAttributeName: font};
    CGSize labelsize = [text boundingRectWithSize:size options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    
    label.frame = CGRectMake(origin.x, origin.y, labelsize.width, labelsize.height);
    label.textColor = textColor;
    label.numberOfLines = 0;
    [label sizeToFit];
    return label;
}


+(instancetype)labelWithOrigin:(CGPoint)origin
                          attributedText:(NSMutableAttributedString *)attributedText
                      textFont:(UIFont *)font
{
    UILabel *label = [[UILabel alloc] init];
    label.attributedText = attributedText;
    label.font = font;
    
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    CGSize size = CGSizeMake(screenW-20,2000); //设置一个行高上限
    NSDictionary *attribute = @{NSFontAttributeName: font};
    CGSize labelsize = [attributedText.mutableString boundingRectWithSize:size options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    
    label.frame = CGRectMake(origin.x, origin.y, labelsize.width, labelsize.height);
    label.numberOfLines = 0;
    [label sizeToFit];
    return label;
}




- (instancetype)setText:(NSString*)text
              textFont:(UIFont *)font
             textColor:(UIColor *)color
{
    self.text = text;
    self.textColor = color;
    self.font = font;
    self.numberOfLines = 0;
    return self;
}
@end
